package com.sys.platform.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.sys.platform.common.utils.StringUtils;
import com.sys.platform.entity.AddressPlatform;
import com.sys.platform.entity.AdressApi;
import com.sys.platform.entity.Camera;
import com.sys.platform.mapper.AddressPlatformMapper;
import com.sys.platform.mapper.AdressApiMapper;
import com.sys.platform.mapper.CameraMapper;
import com.sys.platform.service.ICameraService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 设备Service业务层处理
 *
 * @author yuanfuqiang
 * @date 2024-07-23
 */
@Slf4j
@Service
public class CameraServiceImpl implements ICameraService {

    @Autowired
    private AdressApiMapper adressApiMapper;

    @Autowired
    private AddressPlatformMapper addressPlatformMapper;

    /**
     * 获取设备流地址
     *
     * @param cameraCode
     * @return
     */
    @Override
    public Object streamUrl(String cameraCode) {
        //获取直播流地址
        AdressApi adressApi = adressApiMapper.selectAdressApiByCode("SfxWishPush");
        if (StringUtils.isNull(adressApi)) {
            throw new RuntimeException("未查询到平台数据，请检查code");
        }
        //查询平台地址
        AddressPlatform platform = new AddressPlatform();
        platform.setType(adressApi.getType());
        platform = addressPlatformMapper.selectOne(platform);
        //地址拼接
        StringBuffer buffer = new StringBuffer();
        buffer.append(platform.getIp());
        buffer.append(":");
        buffer.append(platform.getPort());
        buffer.append(adressApi.getUrl());
        String url = buffer.toString();

        Map<String, Object> map = new HashMap<>();
        map.put("did", cameraCode);
        String result = HttpUtil.get(url, map);
        Object obj = JSONObject.parseObject(result).get("url");
        log.info("设备流地址url：{},参数：{},返回结果：{}", url, map, obj);
        if (StringUtils.isNull(obj)) {
            throw new RuntimeException("获取直播流地址异常");
        }
        return obj;
    }

    @Override
    public Camera getCameraByIp(String ip) {
        return null;
    }
}
